package com.hmkj.admin.controller.systemNotices;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.sysNotice.SystemNotice;
import com.hmkj.core.service.sysNotice.SystemNoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class SystemNoticesJsonController extends GenericController<Integer,SystemNotice>{
    @Resource
    private SystemNoticeService systemNoticeService;
    /**
     * 客户列表列表json数据
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("systemNotice/json/list")
    public DtGrid userListJson(String dtGridPager) throws Exception {
        return systemNoticeService.getDtGridList(dtGridPager, SystemNotice.class);
    }

    /**
     * 新增公告
     * @param systemNotice
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("systemNotice/json/add")
    @RequiresPermissions("sys:system:save")
    public ResultEntity addJson(SystemNotice systemNotice) throws  Exception{
       checkModel(systemNotice);
        if (systemNoticeService.selectCount(new SystemNotice(t -> {
            t.setTitle(systemNotice.getTitle());
        }))>0) {
            throw new BussinessException("标题已存在");
        }
        systemNotice.setStatus(SystemNotice.STATUS.WAIT.code);
        systemNotice.setCreator(AdminSessionHelper.getCurrAdmin().getName());
        systemNoticeService.insert(systemNotice);
        return ok();
    }

    /**
     * 编辑公告
     * @param systemNotice
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("systemNotice/json/edit")
    @RequiresPermissions("sys:system:edit")
    public ResultEntity editJson(SystemNotice systemNotice) throws  Exception{
       checkModel(systemNotice);
       SystemNotice systemNotice1=checkPrimaryKey(systemNotice.getId());
        if (!systemNotice1.getTitle().equals(systemNotice.getTitle())) {
            if (systemNoticeService.selectCount(new SystemNotice(t -> {
                t.setTitle(systemNotice.getTitle());
            })) > 0) {
                throw new BussinessException("标题已存在");
            }
        }
        systemNotice.setUpdator(AdminSessionHelper.getCurrAdmin().getName());
        systemNoticeService.updateByPrimaryKeySelective(systemNotice);
        return ok();
    }


    /**
     * 发布
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "systemNotice/json/publish")
    @RequiresPermissions("sys:system:publish")
    public ResultEntity publish(Integer id) throws Exception {
        SystemNotice systemNotice=checkPrimaryKey(id);
        if (systemNotice.getStatus() == SystemNotice.STATUS.PUBLISHED.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        systemNoticeService.updateByPrimaryKeySelective(new SystemNotice(a -> {
            a.setId(systemNotice.getId());
            a.setPublishTime(new Date());
            a.setStatus(SystemNotice.STATUS.PUBLISHED.code);
        }));
        return ok();
    }

    /**
     * 撤回
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "systemNotice/json/back")
    @RequiresPermissions("sys:system:back")
    public ResultEntity back(Integer id) throws Exception {
        SystemNotice systemNotice=checkPrimaryKey(id);
        if (systemNotice.getStatus() != SystemNotice.STATUS.PUBLISHED.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        systemNoticeService.updateByPrimaryKeySelective(new SystemNotice(a -> {
            a.setId(systemNotice.getId());
            a.setStatus(SystemNotice.STATUS.BACK.code);
        }));
        return ok();
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "systemNotice/json/del")
    @RequiresPermissions("sys:system:del")
    public ResultEntity del(Integer id) throws Exception {
        SystemNotice systemNotice=checkPrimaryKey(id);
        if (systemNotice.getStatus() == SystemNotice.STATUS.PUBLISHED.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        systemNoticeService.updateByPrimaryKeySelective(new SystemNotice(a -> {
            a.setId(systemNotice.getId());
            a.setDelFlag(SystemNotice.DELFLAG.DELETED.code);
        }));
        return ok();
    }

    private SystemNotice checkPrimaryKey(Integer id) throws Exception {
        if (null == id) {
            throw new BussinessException("参数有误");
        } else {
            SystemNotice systemNotice = systemNoticeService.selectOne(new SystemNotice(t -> {
                t.setId(id);
                t.setDelFlag(SystemNotice.DELFLAG.NORMAL.code);
            }));
            if (null != systemNotice) {
                return systemNotice;
            } else {
                throw new BussinessException("参数有误");
            }
        }
    }

    private void checkModel(SystemNotice systemNotice){
        if(StringUtils.isBlank(systemNotice.getTitle())){
            throw new BussinessException("标题不能为空");
        }else if(systemNotice.getTitle().length()>100){
            throw new BussinessException("标题长度不能超过100位");
        }
        else if(StringUtils.isBlank(systemNotice.getPublisher())){
            throw new BussinessException("发布者不能为空");
        }
        else if(systemNotice.getPublisher().length()>30){
            throw new BussinessException("发布者长度不能超过30位");
        }
        else if(StringUtils.isBlank(systemNotice.getContent())){
            throw new BussinessException("资讯内容不能为空");
        }
        else if(systemNotice.getContent().toString().length()> 200){
            throw new BussinessException("内容不能超过200位");
        }
    }
}
